/* control the observer  thread */
package Observing;
//if you want to update listener, do stop and then start
import java.util.ArrayList;
import java.util.Iterator;
import Algo.Algo;
import DataBase.MenuTag_File;
import DirectsDB.MenuDirects;


public class Observing {
	private ArrayList<String> sourceList=new ArrayList<String>();
    private Thread observerThread;
    private int flag=0;
    private MenuDirects dirDB;
    public Observing(){}
	public void addSource(String source,MenuDirects dirDB){
//		if (!sourceList.contains(source)){
//			sourceList.add(source);
//		}	
		String temp=source;
		temp=temp.replaceAll("\\\\", "\\\\\\\\");
		dirDB.addDir(temp);
		
			
	}
	public void deleteSource(String source ,MenuDirects dirDB){
//		if (sourceList.contains(source)){
//			sourceList.remove(source);
//		}
		String temp=source;
		dirDB.delDir(temp);
	}
	public String[] returnAllSource(MenuDirects dirDB){
		
//		int size=sourceList.size();
//		String[] returnStr=new String[size];
//		Iterator<String> iter = sourceList.iterator();
//		int i=0;
//		while (iter.hasNext()){
//			
//			returnStr[i]=iter.next();
//			i++;
//		}
//		String[] temp=dirDB.retAllDir();
//		System.out.println("Size:   "+returnStr.length+  "  "+temp.length);
//		for(i=0; i<temp.length; i++)
//		{
//			System.out.println("returnStr : "+i+" "+returnStr[i]);
//			System.out.println("temp : "+i+" "+temp[i]);
//			System.out.println("");
//		}
//		return returnStr;	
		return dirDB.retAllDir();
	}
	public String[] returnAllSourceForPath(MenuDirects dirDB)
	{
		String[] returnStr=dirDB.retAllDir();
		String temp;
		int i, size=returnStr.length;
		for(i=0; i<size; i++)
		{
			temp=returnStr[i];
			temp=temp.replaceAll("////////", "////");
		}
		return returnStr;
		
	}
	public void startObserver(ArrayList<Algo> algoList,MenuTag_File dataBase,MenuDirects dirDB){
		stopObserver();
		flag=1;
		observerThread=new ObserverThread(returnAllSourceForPath(dirDB),algoList,dataBase,dirDB);
		observerThread.start();	
	}
	public void stopObserver(){
		if( flag!=0)
		{
			((ObserverThread) observerThread).die();
		}
	}
}